<template>
  <div class="zhuche">
    <hm-header style="background-color: #fff">面经注册</hm-header>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button color="#07c160" block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
      <div class="denglu">
        <router-link to="/denglu">已有账号,去登录</router-link>
      </div>
    </van-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "ZhuChe",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  mounted() {
    console.log(this.username);
  },
  methods: {
    //发送post请求
    onSubmit() {
      axios({
        url: "http://interview-api-t.itheima.net/h5/user/register",
        method: "post",
        data: {
          username: this.username,
          password: this.password,
        },
      })
        .then((res) => {
          console.log(res);
          if (res.data.code === 10000) {
            this.$toast.success("注册成功");
            this.$router.push("/denglu");
          }
        })
        .catch((err) => {
          console.log(err);
          this.$toast.fail("注册失败");
          
        }) ;
    },
    formatter(value) {
      return value.replace(/[^\d]/g, "");
    },
  },
};
</script>

<style lang="less" scoped>
.denglu {
  text-align: right;
  margin-top: 10px;
  margin-right: 20px;
  a {
    font-size: 12px;
    color: #006699;
  }
}
</style>
